//
//  BVUserSuggestViewController.m
//  BuyVegetables1
//
//  Created by 叶思超 on 15/10/23.
//  Copyright (c) 2015年 叶思超. All rights reserved.
//

#import "BVUserSuggestViewController.h"
#import "BVTextView.h"
#import "BVConst.h"
#import "HttpTool.h"
#import "MBProgressHUD+MJ.h"

@interface BVUserSuggestViewController ()
/**
 *  输入框
 */
@property (nonatomic, strong) BVTextView *textView;
@end

@implementation BVUserSuggestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    // 设置导航栏
    [self setupNav];
    
    // 添加输入框
    [self setupTextField];
}

#pragma mark -- 添加输入框
- (void)setupTextField {
    self.textView = [[BVTextView alloc] init];
    self.textView.layer.borderColor = [BVBackgroundLightGrayColor CGColor];
    self.textView.layer.borderWidth = 1;
    self.textView.layer.masksToBounds = YES;
    self.textView.layer.cornerRadius = 6.0;
    self.textView.placeholder = @"请填写您的宝贵意见...";
    self.textView.placeholderColor = BVTextLightGrayColor;
    self.textView.frame = CGRectMake(5, 69, BVScreenW-10, 180);
    self.textView.textColor = BVBaseTextBlackColor;
    [self.view addSubview:self.textView];
}

#pragma mark -- 确定
- (void)correctClick {
    [self.textView resignFirstResponder];
    
    // 拼接路径
    NSString *url = [NSString stringWithFormat:@"%@/VegetableUserAPI/doUserFeedBackById", Path];
    
    // 拼接参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *userinfo = [defaults dictionaryForKey:@"USERINFO"];
    NSString *user_id = userinfo[@"id"];
    if (user_id != nil) {
        params[@"user_id"] = user_id;
        
        if (self.textView.text.length == 0) {
            [MBProgressHUD showError:@"请输入您的意见" toView:self.view];
        } else {
            params[@"content"] = self.textView.text;
            [HttpTool get:url params:params success:^(id responseObj) {
                if ([responseObj[@"code"] isEqualToString:@"1"]) {
                    BVLog(@"用户提交反馈成功 ---%@", responseObj);
                    [MBProgressHUD showMessage:@"您的意见正在提交" toView:self.view];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        // 移除蒙板
                        [MBProgressHUD hideHUD];
                        
                        // 返回上个界面
                        ROOTNAVPOP
                    });
                    
                } else {
                    BVLog(@"用户提交反馈信息失败====%@", responseObj);
                }
            } failure:^(NSError *error) {
                BVLog(@"请求失败======%@", error);
            }];
        }
    } else {
        
    }
}
#pragma mark -- touch事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}
#pragma mark -- 设置导航栏
- (void)setupNav {
    self.title = @"意见反馈";
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(backClick)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(correctClick)];
}

#pragma mark -- 返回
- (void)backClick {
    ROOTNAVPOP
}
@end
